<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>31_节点操作</title>
    <link rel="stylesheet" href="./css/basic.css">
    <style>
        .container {
            width: 100%;
            height: 100vh;
            background-color: pink;
            padding: 10px;
            box-sizing: border-box;
        }
        .content {
          width: 100%;
          height: 75%;
          background-color: #fff;
          padding: 20px;
          box-sizing: border-box;
        }
        .input {
            height: 20%;
            width: 100%;
            position: relative;
            margin-top: 20px;
        }
        .input textarea,   .input button{
            position: absolute;
            resize: none;
        }
        .input textarea {
            width: 100%;
            height: 100%;
            padding: 10px;
            box-sizing: border-box;
        }
        .input button {
            right: 10px;
            bottom: 10px;
            width: 100px;
            height: 50px;
            background-color: blue;
            color: #fff;
            font-size: 20px
        }
        .message {
            color:brown;
            font-weight: bolder;
            font-size: 30px;
            margin-right: 20px;
        }
        .datetime {
            color: #000;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 聊天区 -->
      <div class="content"></div>
      <!-- 输入区 -->
      <div class="input">
        <textarea name="" id="contentArea"></textarea>
        <button  id="btnSend">发送</button>
      </div>
    </div>
    <script>
      const btnDom = document.querySelector("#btnSend") // 发送按钮对象
      const textareaDom = document.querySelector("#contentArea") // 输入区对象
      const contentDom = document.querySelector(".content") // 聊天区对象
      enableBtn()
      textareaDom.addEventListener("input", (event) => {
        enableBtn() // 控制按钮是否能点击
      })
      btnDom.addEventListener("click", () => {
        sendMsg()
      })

      textareaDom.addEventListener("keydown", (event) => {
        if(event.key === "Enter") {
          sendMsg()
        }
      })
      function enableBtn () {
        if(textareaDom.value) {
            // 删除disabled属性
            btnDom.removeAttribute("disabled")
        }else {
            // 加上属性
            btnDom.setAttribute("disabled", true)
        }
      }
      // 发送消息
      function sendMsg () {
       // if(textareaDom.value)
       const content = textareaDom.value // 聊天的信息
    //    contentDom.append()
       const p = document.createElement("p")
         p.innerHTML = `<span  class='message'>${content}</span><span class='datetime'>${new Date().toLocaleString()}</span>`

         contentDom.append(p)
         textareaDom.value = ""
      }

    </script>
</body>
</html>